package com.ftg.learn.chapter12.springbootdemo.service.impl;

import com.ftg.learn.chapter12.springbootdemo.entity.UserEntity;
import com.ftg.learn.chapter12.springbootdemo.mapper.UserMapper;
import com.ftg.learn.chapter12.springbootdemo.service.UserService;
import com.ftg.learn.chapter12.springbootdemo.util.R;
import com.ftg.learn.chapter12.springbootdemo.vo.UserVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实现类
 *
 * @author 布衣
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public R<Integer> login(String username, String password) {
        if (username == null || username.isEmpty()) {
            return R.failure("用户名不能为空");
        }
        if (password == null || password.isEmpty()) {
            return R.failure("密码不能为空");
        }
        Integer id = userMapper.login(username, password);
        if (id == null) {
            return R.failure("用户或者密码错误!!!");
        }
        return R.success(id);
    }

    @Override
    public R<PageInfo<UserVo>> list(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<UserVo> list = userMapper.list();
        return R.success(new PageInfo(list));
    }

    @Override
    public R<UserVo> getUserInfoById(Integer id) {
        return R.success(userMapper.getUserInfoById(id));
    }

    @Transactional
    @Override
    public R<Boolean> modifyPassword(Integer id, String oldpasswrod, String newpasswrod) {
        //查看用户是否存在
        UserEntity ue = userMapper.findOneById(id);
        if (ue == null) {
            return R.build(500, "用户不存在", false);
        }
        if (!ue.getPassword().equals(oldpasswrod)) {
            return R.build(500, "密码错误", false);
        }
        //调用日志管理类方法完成日志的添加
//        自动执行
        return userMapper.modifyPassword(id, newpasswrod) > 0 ? R.build(200, "修改成功", true) : R.build(500, "修改失败", false);
    }

    @Override
    public R<Integer> save(UserEntity ue) {
        return R.success(userMapper.save(ue));
    }

    @Override
    public R<Boolean> modify(UserVo ue) {
        return R.success(userMapper.modify(ue));
    }

    @Override
    public R<Boolean> delById(Integer id) {
        return R.success(userMapper.delById(id));
    }

    public static void main(String[] args) {
        int[] as = {6,17,32,72,7,3,4,2};
        // as.length
        Arrays.sort(as);

        int i = Arrays.binarySearch(as, 7);

//        for (int i = 0; i < as.length; i++) {
//            if(as[i] == 7){
//                System.out.println("e ");
//                break;
//            }
//        }

        if(i > 0){
            //负载均衡  application : 次数
        }



    }

}
